<template>
  <div class="flex">
    <!-- 左侧图号列表 -->
    <div class="w-60 mr-4">
      <el-card class="box-card" shadow="never">
        <template #header>
          <span>图号列表</span>
        </template>
        <el-input v-model="filterDrawingNo" placeholder="搜索图号" class="mb-3" clearable />
        <el-scrollbar height="calc(100vh - 250px)">
          <el-menu :default-active="activeDrawingNoId" @select="handleDrawingNoSelect">
            <el-menu-item v-for="item in filteredDrawingNoList" :key="item.id" :index="String(item.id)">
              {{ item.drawingNo }}
            </el-menu-item>
          </el-menu>
        </el-scrollbar>
      </el-card>
    </div>

    <!-- 右侧主内容 -->
    <div class="flex-1">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="模具名" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入模具名" clearable @keyup.enter="handleQuery"
              class="!w-240px" />
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model="queryParams.code" placeholder="请输入编码" clearable @keyup.enter="handleQuery"
              class="!w-240px" />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
              start-placeholder="开始日期" end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px" />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" /> 搜索
            </el-button>
            <el-button @click="resetQuery">
              <Icon icon="ep:refresh" class="mr-5px" /> 重置
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" border
          resizable>
          <!--           <el-table-column label="图号" align="center" prop="drawingNo" /> -->
          <el-table-column label="模具名" align="center" prop="name" />
          <el-table-column label="编码" align="center" prop="code" />
          <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
          <el-table-column label="操作" align="center" min-width="120px" fixed="right">
            <template #default="scope">
              <el-button link type="primary" @click="openUpkeepSet(scope.row)" v-hasPermi="['mold:upkeep-set:set']">
                保养设置
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </ContentWrap>
    </div>
  </div>

  <!-- 保养设置弹窗 -->
  <UpkeepSetForm ref="upkeepSetFormRef" @success="handleUpkeepSetSuccess" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UpkeepSetApi, type Account } from '@/api/mold/upkeepset'
import { DrawingNoApi, type DrawingNo } from '@/api/mold/drawingno'
import UpkeepSetForm from './upkeepset.vue'

/** 模具台账 列表 */
defineOptions({ name: 'Account' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Account[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  code: undefined,
  drawingNoId: undefined, // 添加图号ID筛选条件
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const drawingNoList = ref<DrawingNo[]>([]) // 图号列表
const filterDrawingNo = ref('') // 图号搜索过滤
const activeDrawingNoId = ref('') // 当前选中的图号ID

// 过滤后的图号列表
const filteredDrawingNoList = computed(() => {
  if (!filterDrawingNo.value) {
    return drawingNoList.value
  }
  return drawingNoList.value.filter(item =>
    item.drawingNo && item.drawingNo.includes(filterDrawingNo.value)
  )
})

/** 查询图号列表 */
const getDrawingNoList = async () => {
  const data = await DrawingNoApi.getDrawingNoAll()
  drawingNoList.value = data

}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UpkeepSetApi.getAccountPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.drawingNoId = undefined
  activeDrawingNoId.value = ''
  handleQuery()
}

/** 选择图号 */
const handleDrawingNoSelect = (index: string) => {
  activeDrawingNoId.value = index
  queryParams.drawingNoId = Number(index)
  queryParams.pageNo = 1
  getList()
}

/** 打开保养设置表单 */
const upkeepSetFormRef = ref()
const openUpkeepSet = (row: Account) => {
  console.log(row)
  upkeepSetFormRef.value.open(row.id, row.upkeepId, row.drawingNoId, row.drawingNoId)
}

/** 保养设置成功回调 */
const handleUpkeepSetSuccess = () => {
  // 刷新列表
  getList()
}

/** 初始化 **/
onMounted(() => {
  getDrawingNoList()
  getList()
})
</script>